<?php
namespace app\appletsapi\controller;

use app\common\model\ProductsType;
use app\common\model\ProductsType as ProductsTypeModel;
use app\common\model\Shops;
use app\appletsapi\validate\Product;
use app\common\model\Products as ProductModel;
use app\common\model\ShopsBanner;
use app\common\model\ShopsSeat;

class Products extends Base
{
//    /**
//     * @route('applets/ProductList')
//     * 商品列表
//     */
    public function ProductList()
    {
        $ProductModel = new ProductModel();
        $ProductsTypeModel = new ProductsTypeModel();
        $list = $ProductsTypeModel->get_tree('type_id,p_id,type_name',['is_del' => 1],'p_id asc,sort asc','offline');
        array_walk($list,function(&$v){
            unset($v['children']);
            $v['product_list'] = [];
        });
        $type_list = array_column($list,null,'type_id');
//        var_dump($type_list);die;
        $validate = new Product();
        $ssort = input('get.ssort');
        $nsort = input('get.nsort');
        if(!$validate->scene('paixu')->check(['ssort'=>$ssort,'nsort'=>$nsort])){
            $this->result('',-1,$validate->getError());
        }
        $pwhere = [
            "shop_id" => $this->_shopid,
            "type_id" => input('get.type_id'),
            "product_name" => input('get.product_name'),
            "state" => 1,
            "approved" => 2,
            "shop_state" => 1,
            "shop_approved" => 1,
            "is_del" => 1,
            "class_id" => 5,
        ];
        $item = 'p.*';
        if($this->_userid){
            $item .= ",(select quantity from ddt_cart where ddt_cart.product_id = p.product_id and ddt_cart.user_id = {$this->_userid}) as cart_number,(select cart_id from ddt_cart where ddt_cart.product_id = p.product_id and ddt_cart.user_id = {$this->_userid}) as cart_id";
        }
        $orderby = [];
        empty($ssort)?'':$orderby['salecount'] = $ssort;
        empty($nsort)?'':$orderby['createat'] = $nsort;
//        var_dump($orderby);die;
        $item = $this->getfieldstring($item);
        $products = $ProductModel->get_product_list($pwhere,$orderby,$item)->each(function($value){
            if(!isset($value['cart_number']) || empty($value['cart_number'])){
                $value['cart_number'] = 0;
            }
            return $value;
        });
        if(!empty($products)){
            $products = $products->toArray();
            foreach($products as $key => $value){
                if(isset($type_list[$value['type_id']])){
                    $type_list[$value['type_id']]['product_list'][] = $value;
                }
            }
        }
        $products_list = array_values($type_list);
//        var_dump($products_list);
        $this->result($products_list,1,'返回成功');
    }

//    /**
//     * @route('applets/SelectList')
//     * 搜索商品列表
//     */
    public function SelectList()
    {
        $ProductModel = new ProductModel();
//        var_dump($type_list);die;
        $pwhere = [
            "shop_id" => $this->_shopid,
            "type_id" => input('get.type_id'),
            "product_name" => input('get.product_name'),
            "state" => 1,
            "approved" => 2,
            "shop_state" => 1,
            "shop_approved" => 1,
            "is_del" => 1,
            "class_id" => 5,
        ];
        $item = 'p.*';
        if($this->_userid){
            $item .= ",(select quantity from ddt_cart where ddt_cart.product_id = p.product_id and ddt_cart.user_id = {$this->_userid}) as cart_number,(select cart_id from ddt_cart where ddt_cart.product_id = p.product_id and ddt_cart.user_id = {$this->_userid}) as cart_id";
        }
        $orderby = [];
        empty($ssort)?'':$orderby['salecount'] = $ssort;
        empty($nsort)?'':$orderby['createat'] = $nsort;
//        var_dump($orderby);die;
        $item = $this->getfieldstring($item);
        $products = $ProductModel->get_product_list($pwhere,$orderby,$item)->each(function($value){
            if(!isset($value['cart_number']) || empty($value['cart_number'])){
                $value['cart_number'] = 0;
            }
            return $value;
        });
//        var_dump($products_list);
        $this->result($products,1,'返回成功');
    }

    /**
     * @route('applets/GetTypeList')
     * 分类列表
     */
    public function GetTypeList()
    {
        $ProductsTypeModel = new ProductsTypeModel();
        $list = $ProductsTypeModel->get_tree('type_id,p_id,type_name',['is_del' => 1],'p_id asc,sort asc','offline');

        $this->result($list,1,'成功');
    }
//
//    /**
//     * @route('applets/Shopinfo')
//     * 商铺详情
//     */
    public function Shopinfo()
    {
        $shopModel = new Shops();
        $shop_id = input('get.shop_id',0);
        $shop = $shopModel->getOneShop($shop_id,'*');
        $data = empty($shop)?null:$shop->toArray();
        if(!empty($shop['reserve_info'])){
            $reserve_info = json_decode($shop['reserve_info'],true);
            unset($shop['reserve_info']);
            $time_ranges = $reserve_info['time_ranges'];
            $data['lunch'] = explode(' - ',$time_ranges[0]);
            $data['dinner'] = explode(' - ',$time_ranges[1]);
            foreach($time_ranges as $value){
                $time_range = explode(" - ",$value);
                $all_time[] = $time_range[0];
                $all_time[] = $time_range[1];
                $this->get_time_array($time_range[1],$time_range[0]);
            }
        }
        $data['now_time'] = time();
        unset($data['reserve_info']);
        $this->result($data,1,'返回成功');
    }

    private $time_callback = [];
    private $time_all_callback = [];

    private function get_time_array($end_time,$begin_time,$MINUTE = 30){
        if($end_time >= $begin_time){
            if($begin_time > date("H:i",time())){
                $this->time_callback[] = $begin_time;
            }
            $this->time_all_callback[] = $begin_time;
//            var_dump($this->time_callback);
            $thetime = date("H:i",strtotime("+$MINUTE MINUTE",strtotime($begin_time)));
            if($thetime == "00:00"){
                return;
            }
            $this->get_time_array($end_time,$thetime);
        }
    }

    function getfieldstring($item){
        if($this->_userid != 0) {
            $userinfo = $this->GetUserInfo();
            if($userinfo['utype'] == 2){
                $item .= ',p.mprice as sale_price';
            } else {
                $item .= ',p.sprice as sale_price';
            }
        } else {
            $item .= ',p.sprice as sale_price';
        }
        return $item;
    }

    /**
     * @route('applets/Seatinfo')
     * 获取座位信息
     */
    public function Seatinfo()
    {
        $seatModel = new ShopsSeat();
        $seatinfo = $seatModel->getseatinfo(['seat_id' => input('post.seat_id')],'st.*,s.shop_name');
        if(!empty($seatinfo)){
            $bannerModel = new ShopsBanner();
            $banners = $bannerModel->get_list(['shop_id'=>$seatinfo['shop_id'],'type'=>1],'sort','*',3);
            $banners = $banners->toArray();
            $seatinfo['shop_banner'] = $banners['data'];
        }
        $this->result($seatinfo,1,'返回成功');
    }
}
